package com.play.ballen.im;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;

import com.google.android.material.snackbar.Snackbar;
import com.hjq.toast.ToastUtils;
import com.hyphenate.chat.EMChatRoom;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.easeui.EaseIM;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.domain.EaseUser;
import com.hyphenate.easeui.model.EaseEvent;
import com.hyphenate.easeui.provider.EaseUserProfileProvider;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.mw.plp.uim.DemoHelper;
import com.mw.plp.uim.GroupHelper;
import com.mw.plp.uim.common.constant.DemoConstant;
import com.mw.plp.uim.common.enums.Status;
import com.mw.plp.uim.common.interfaceOrImplement.OnResourceParseCallback;
import com.mw.plp.uim.common.net.Resource;
import com.play.ballen.R;
import com.play.ballen.databinding.ActivityChatBinding;
import com.play.ballen.im.fragment.ChatFragment;
import com.play.ballen.im.viewModel.ChatViewModel;
import com.play.ballen.im.viewModel.GroupDetailViewModel;
import com.play.ballen.mall.ui.im.vm.MessageViewModel;
import com.yuven.baselib.component.BaseMVVMActivity;
import com.yuven.baselib.component.viewmodel.BaseViewModel;

public class ChatActivity extends BaseMVVMActivity<BaseViewModel, ActivityChatBinding> implements ChatFragment.OnFragmentInfoListener {


    private TextView tvTitle;
    private TextView subTitle;

    private TextView tvStatus;

    private ImageView ivBtnMenu;

    private String conversationId;
    private int chatType;
    private ChatFragment fragment;
    private String historyMsgId;
    private ChatViewModel viewModel;
    private GroupDetailViewModel groupDetailViewModel;


    public static void actionStart(Context context, String conversationId, int chatType) {
        Intent intent = new Intent(context, ChatActivity.class);
        intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID, conversationId);
        intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, chatType);
        context.startActivity(intent);
    }
    @Override
    protected void initIntent(Intent intent) {
        conversationId = intent.getStringExtra(EaseConstant.EXTRA_CONVERSATION_ID);
        chatType = intent.getIntExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_SINGLE);
        historyMsgId = intent.getStringExtra(DemoConstant.HISTORY_MSG_ID);
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_chat;
    }

    @Override
    protected Class<BaseViewModel> viewModelClass() {
        return BaseViewModel.class;
    }

    @Override
    protected void initView() {
        tvTitle = findViewById(R.id.tv_title);
        subTitle = findViewById(R.id.tv_sub_title);
        tvStatus = findViewById(R.id.tv_status);
        ivBtnMenu =findViewById(R.id.iv_btn_more);
        initChatFragment();

        findViewById(R.id.iv_btn_back).setOnClickListener(v -> {
            onBackPressed();
        });
        findViewById(R.id.iv_btn_more).setOnClickListener(v -> {
            ChatSettingActivity.actionStart(this,conversationId);
            if(chatType == DemoConstant.CHATTYPE_SINGLE) {
                //跳转到单聊设置页面
                //SingleChatSetActivity.actionStart(mContext, conversationId);
            }else {
                // 跳转到群组设置
                if(chatType == DemoConstant.CHATTYPE_GROUP) {
                    // GroupDetailActivity.actionStart(mContext, conversationId);
                }else if(chatType == DemoConstant.CHATTYPE_CHATROOM) {
                    //ChatRoomDetailActivity.actionStart(mContext, conversationId);
                }
            }
        });
        fragment.setOnFragmentInfoListener(this);
    }

    @Override
    protected void initData() {
        EMConversation conversation = EMClient.getInstance().chatManager().getConversation(conversationId);
        MessageViewModel messageViewModel = new ViewModelProvider(this).get(MessageViewModel.class);
        groupDetailViewModel = new ViewModelProvider(this).get(GroupDetailViewModel.class);
        viewModel = new ViewModelProvider(this).get(ChatViewModel.class);
        viewModel.getDeleteObservable().observe(this, response -> {
            parseResource(response, new OnResourceParseCallback<Boolean>() {
                @Override
                public void onSuccess(Boolean data) {
                    finish();
                    EaseEvent event = EaseEvent.create(DemoConstant.CONVERSATION_DELETE, EaseEvent.TYPE.MESSAGE);
                    messageViewModel.setMessageChange(event);
                }
            });
        });
        viewModel.getChatRoomObservable().observe(this, response -> {
            parseResource(response, new OnResourceParseCallback<EMChatRoom>() {
                @Override
                public void onSuccess(@Nullable EMChatRoom data) {
                    setDefaultTitle();
                }
            });
        });
        messageViewModel.getMessageChange().with(DemoConstant.GROUP_CHANGE, EaseEvent.class).observe(this, event -> {
            if(event == null) {
                return;
            }
            if(event.isGroupLeave() && TextUtils.equals(conversationId, event.message)) {
                finish();
            }
        });
        messageViewModel.getMessageChange().with(DemoConstant.CHAT_ROOM_CHANGE, EaseEvent.class).observe(this, event -> {
            if(event == null) {
                return;
            }
            if(event.isChatRoomLeave() && TextUtils.equals(conversationId,  event.message)) {
                finish();
            }
        });
        messageViewModel.getMessageChange().with(DemoConstant.MESSAGE_FORWARD, EaseEvent.class).observe(this, event -> {
            if(event == null) {
                return;
            }
            if(event.isMessageChange()) {
                showSnackBar(event.event);
            }
        });
        messageViewModel.getMessageChange().with(DemoConstant.CONTACT_CHANGE, EaseEvent.class).observe(this, event -> {
            if(event == null) {
                return;
            }
            if(conversation == null) {
                finish();
            }
        });
        groupDetailViewModel.getGroupObservable().observe(this, response-> {
            parseResource(response, new OnResourceParseCallback<EMGroup>() {
                @Override
                public void onSuccess(@Nullable EMGroup data) {
                    String extension = data.getExtension();
                    if(!TextUtils.equals("default", extension)) {
                        subTitle.setText("该群仅供试用，72小时后将被删除");
                        subTitle.setVisibility(View.VISIBLE);
                    }
                }
            });
        });

        setDefaultTitle();
        checkGroupInfo();
    }



    private void initChatFragment() {
        fragment = new ChatFragment();
        Bundle bundle = new Bundle();
        bundle.putString(EaseConstant.EXTRA_CONVERSATION_ID, conversationId);
        bundle.putInt(EaseConstant.EXTRA_CHAT_TYPE, chatType);
        bundle.putString(DemoConstant.HISTORY_MSG_ID, historyMsgId);
        bundle.putBoolean(EaseConstant.EXTRA_IS_ROAM, DemoHelper.getInstance().getModel().isMsgRoaming());
        fragment.setArguments(bundle);
        getSupportFragmentManager()
                .beginTransaction()
                .replace(R.id.fl_fragment, fragment, "chat").commit();
    }

    private void setTitleBarRight() {
        if(chatType == DemoConstant.CHATTYPE_SINGLE) {
            //titleBarMessage.setRightImageResource(R.drawable.chat_user_info);
        }else {
            //titleBarMessage.setRightImageResource(R.drawable.chat_group_info);
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if(intent != null) {
            initIntent(intent);
            initChatFragment();
            initData();
        }
    }





    private void checkGroupInfo() {
        if(chatType == DemoConstant.CHATTYPE_GROUP) {
            EMGroup group = EMClient.getInstance().groupManager().getGroup(conversationId);
            if(group == null || TextUtils.isEmpty(group.getExtension())) {
                groupDetailViewModel.getGroup(conversationId);
            }else {
                if(!TextUtils.equals("default", group.getExtension())) {
                    subTitle.setText("该群仅供试用，72小时后将被删除");
                    subTitle.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    private void showSnackBar(String event) {

    }

    private void setDefaultTitle() {
        String title;
        if(chatType == DemoConstant.CHATTYPE_GROUP) {
            title = GroupHelper.getGroupName(conversationId);
        }else if(chatType == DemoConstant.CHATTYPE_CHATROOM) {
            EMChatRoom room = EMClient.getInstance().chatroomManager().getChatRoom(conversationId);
            if(room == null) {
                viewModel.getChatRoom(conversationId);
                return;
            }
            title =  TextUtils.isEmpty(room.getName()) ? conversationId : room.getName();
        }else {
            EaseUserProfileProvider userProvider = EaseIM.getInstance().getUserProvider();
            if(userProvider != null) {
                EaseUser user = userProvider.getUser(conversationId);
                if(user != null) {
                    title = user.getNickname();
                }else {
                    title = conversationId;
                }
            }else {
                title = conversationId;
            }
        }
        this.tvTitle.setText(title);
    }

    @Override
    public void onChatError(int code, String errorMsg) {
        ToastUtils.show(errorMsg);
    }

    @Override
    public void onOtherTyping(String action) {
        if (TextUtils.equals(action, "TypingBegin")) {
            this.tvTitle.setText(getString(com.hyphenate.easeui.R.string.alert_during_typing));
        }else if(TextUtils.equals(action, "TypingEnd")) {
            setDefaultTitle();
        }
    }
    public <T> void parseResource(Resource<T> response, @NonNull OnResourceParseCallback<T> callback) {
        if(response == null) {
            return;
        }
        if(response.status == Status.SUCCESS) {
            callback.hideLoading();
            callback.onSuccess(response.data);
        }else if(response.status == Status.ERROR) {
            callback.hideLoading();
            if(!callback.hideErrorMsg) {
                ToastUtils.show(response.getMessage());
            }
            callback.onError(response.errorCode, response.getMessage());
        }else if(response.status == Status.LOADING) {
            callback.onLoading(response.data);
        }
    }
}
